<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>

	<div th:insert="/common.html"></div>

	<!--图片上传-->
	<link rel="stylesheet" type="text/css" href="/plugins/bootstrap-fileinput/css/fileinput.min.css">
	<script type="text/javascript" src="/plugins/bootstrap-fileinput/js/fileinput.min.js"></script>
	<script type="text/javascript" src="/plugins/bootstrap-fileinput/js/locales/zh.js"></script>
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button onclick="newlyBuild()" type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='onStatus()'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='closeStatus()'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
<!--			                      <thead>-->
<!--			                          <tr>-->
<!--			                              <th class="" style="padding-right:0px">-->
<!--			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">-->
<!--			                              </th> -->
<!--										  <th class="sorting_asc">广告ID</th>-->
<!--									      <th class="sorting">分类ID</th>-->
<!--									      <th class="sorting">标题</th>-->
<!--									      <th class="sorting">URL</th>		-->
<!--									      <th class="sorting">图片</th>	-->
<!--									      <th class="sorting">排序</th>		-->
<!--									      <th class="sorting">是否有效</th>											     						      							-->
<!--					                      <th class="text-center">操作</th>-->
<!--			                          </tr>-->
<!--			                      </thead>-->
<!--			                      <tbody>-->
<!--			                          <tr>-->
<!--			                              <td><input  type="checkbox"></td>			                              -->
<!--				                          <td>1</td>-->
<!--									      <td>1</td>-->
<!--									      <td>促销海报1</td>-->
<!--									      <td>http://wwww.hb.com/hd1.html</td>-->
<!--									      <td>-->
<!--									      	<img alt="" src="" width="100px" height="50px">-->
<!--									      </td>-->
<!--									      <td>1</td>-->
<!--									      <td>有效</td>									     								     -->
<!--		                                  <td class="text-center">                                           -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
<!--			                      </tbody>-->
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
						<input name="id" type="hidden">
		      			<select name="categoryId" id="category" class="form-control" >
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input name="title"  class="form-control" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input name="url"  class="form-control" placeholder="URL" >  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input name="sortOrder"  class="form-control" placeholder="排序" >  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td colspan="2">
									<input type="text" name="pic">
									<input type="file" id="myFile" name="myFile"><!-- multiple支持多个文件上传 -->
					            </td>
							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input name="status" value="2" type="checkbox" class="icheckbox_square-blue" >
		      		</td>
		      	</tr>  	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button id="insetId" onclick="insertGet()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
			<button id="updateId" onclick="updateGet()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">修改</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

</body>
<script type="text/javascript">
	$(function () {

		$('#dataList').bootstrapTable({
			url: "/content/getContentAll", //请求后台的URL（*）
			method: 'get',                      //请求方式（*）
			// contentType: "application/x-www-form-urlencoded",/*使用post*/
			// toolbar: '#sdfname',              //工具按钮用哪个容器
			striped: true,                      //是否显示行间隔色
			cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
			pagination: true,                   //是否显示分页（*）
			sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
			pageNumber: 1,                      //初始化加载第一页，默认第一页,并记录
			pageSize: 10,                        //每页的记录行数（*）
			pageList: [1, 3, 5],                //可供选择的每页的行数（*）
			showColumns: true,                  //是否显示所有的列（选择显示的列）
			showRefresh: true,                  //是否显示刷新按钮
			minimumCountColumns: 1,             //最少允许的列数
			clickToSelect: true,                //是否启用点击选中行
			showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
			detailView: true,                  //是否显示父子表
			//得到查询的参数
			queryParams : function (params) {
			},
			columns: [{
				checkbox: true,
				visible: true                  //是否显示复选框
			}, {
				field: 'id',
				title: '广告ID'
			}, {
				field: 'categoryId',
				title: '广告类目ID'
			}, {
				field: 'title',
				title: '标题'
			}, {
				field: 'url',
				title: '链接'
			}, {
				field: 'sortOrder',
				title: '排序'
			}, {
				field: 'status',
				title: '是否有效',
				formatter:function (value, row, index) {
					if (value == 1){
						return "有效";
					}else if (value == 2){
						return "无效";
					}
				}
			},{
				field:'id',
				title: '操作',
				width: 120,
				align: 'center',
				valign: 'middle',
				formatter: function (value, row, index) {
					var id = value;
					var result = "";
					result += '<button onclick="updateEcho('+id+')" type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>';
					result += "<a href='javascript:DeleteById(" + id + ")' class='btn btn-xs red' title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
					return result;
				}
			}
			],
			onLoadError: function () {
				toastr.error("数据加载失败！");
			},onDblClickRow: function (row, $element) {//双击事件
			}
		})
	})
	$('#myFile').fileinput({
		language: 'zh',//汉化
		uploadUrl:"/fileController/uploadFile",//上传地址
		browseClass: 'btn btn-primary',
		allowedFileExtensions:['jpg','png','gif','txt','pdf','xls','xlsx','ico'], //限制文件上传类型
		maxFileSize:2048,//设置文件上传大小 单位kb
		maxFileCount:1,//最大上传数量
		showUploadedThumbs:false,
		enctype: "multipart/form-data"
	});
	//上传成功回调
	$('#myFile').on('fileuploaded',function(event, data, previewId, index){
		console.log(data);
		if(data.response != null){
			$("[name='pic']").val(data.response.data);//abc.jpg  + "," + bcd.jpg
		}
	})
	/*查询全部广告类型*/
	function selectCategoryAll(id) {
		$.ajax({
			url: "/contentCategory/getContentCategoryAll",
			dataType: "json",
			type: "post",
			data: {},
			async:false,
			success:function (result) {
				var sed = "";
				var html = '';
					html += '<option value="-1">--请选择--</option>';
				for (var i = 0; i < result.length; i++) {
					if (id == result[i].id){
						sed = "selected";
					}else {
						sed = "";
					}
					html += '<option value="'+result[i].id+'" '+sed+'>'+result[i].name+'</option>'
				}
				$("#category").html(html);
			},error:function (result) {
				alert(result.message)
			}
		})
	}
	/*查询*/
	function query() {
		$("#dataList").bootstrapTable("refresh");
	}
	function newlyBuild() {
		$('#updateId').hide();
		$('#insetId').show();
		selectCategoryAll(-1);
	}
	function insertGet() {
		if ($("[name='status']").prop('checked')){
			status = 1;
		}else{
			status = 2;
		}
		var sortOrder = $("[name='sortOrder']").val();
		$.ajax({
			url: "/content/addContent",
			dataType: "json",
			type: "post",
			data: {
				categoryId:$("[name='categoryId']").val(),
				title:$("[name='title']").val(),
				pic:$("[name='pic']").val(),
				url:$("[name='url']").val(),
				status:status,
				sortOrder:Number(sortOrder)
			},
			async:false,
			success:function (result) {
				if (result.code == 10000){
					query();
					toastr.success(result.message)
				}else if(result.code == 9999){
					toastr.error(result.message)
				}
			},error:function (result) {
				alert(result.message)
			}
		})
	}
	function updateEcho(id) {
		$('#insetId').hide();
		$('#updateId').show();
		$.ajax({
			url: "/content/updateContentEcho",
			dataType: "json",
			type: "post",
			data: {
				id:id
			},
			async:false,
			success:function (result) {
				var content = result.data;
				if (result.code == 10000){
					selectCategoryAll(content.categoryId);
					// $("[name='categoryId']").val(content.categoryId);
					$("[name='title']").val(content.title);
					$("[name='pic']").val(content.pic);
					$("[name='status']").val(content.status);
					$("[name='id']").val(content.id);
					if (content.sortOrder == 1){
						$("[name='sortOrder']").checked(true);
					}
				}else if(result.code == 9999){
					toastr.error(result.message)
				}
			},error:function (result) {
				alert(result.message)
			}
		})
	}
	function updateGet() {
		$('#insetId').hide();
		$('#updateId').show();
		if ($("[name='status']").prop('checked')){
			status = 1;
		}else{
			status = 2;
		}
		var sortOrder = $("[name='sortOrder']").val();
		$.ajax({
			url: "/content/updateContent",
			dataType: "json",
			type: "post",
			data: {
				id:$("[name='id']").val(),
				categoryId:$("[name='categoryId']").val(),
				title:$("[name='title']").val(),
				pic:$("[name='pic']").val(),
				url:$("[name='url']").val(),
				status:status,
				sortOrder:Number(sortOrder)
			},
			async:false,
			success:function (result) {
				var content = result.data;
				if (result.code == 10000){
					query();
					toastr.success(result.message)
				}else if(result.code == 9999){
					toastr.error(result.message)
				}
			},error:function (result) {
				alert(result.message)
			}
		})
	}
	function DeleteById(id) {
		if (confirm("确定要删除吗?")) {
			$.ajax({
				url: "/content/deleteContent",
				type: "post",
				dataType: "json",
				data: {
					id:id
				},
				async: false,
				success: function (result) {
					if (result.code == 10000) {
						toastr.success(result.message);
						query();
					} else if (result.code == 9999){
						toastr.error(result.message);
					}
				},
				error: function (result) {
					toastr.error("错误");
				}
			})
		}
	}
	function onStatus() {
		if (confirm("你确认要开启吗？")) {
			var rows = $("#dataList").bootstrapTable("getSelections");
			if (rows.length == 0){
				toastr.error("请选择要开启的数据!");
				return;
			}
			var ids = new Array();
			$(rows).each(function () {
				ids.push(this.id);
			})
			var titleName = "开启";
			$.ajax({
				url:"/content/onOperationContent/",
				type:"post",
				dataType:"json",
				data:"ids[]="+ids,
				async:false,
				success:function (result) {
					if (result){
						query();
						toastr.success(titleName+"成功");
					}else {
						toastr.error(titleName+"失败");
					}
				},
				error:function (result) {
					toastr.error("错误");
				}
			})
		}
	}
	function closeStatus() {
		if (confirm("你确认要屏蔽吗？")) {
			var rows = $("#dataList").bootstrapTable("getSelections");
			if (rows.length == 0){
				toastr.error("请选择要屏蔽的数据!");
				return;
			}
			var ids = new Array();
			$(rows).each(function () {
				ids.push(this.id);
			})
			var titleName = "屏蔽";
			$.ajax({
				url:"/content/closeOperationContent/",
				type:"post",
				dataType:"json",
				data:"ids[]="+ids,
				async:false,
				success:function (result) {
					if (result){
						query();
						toastr.success(titleName+"成功");
					}else {
						toastr.error(titleName+"失败");
					}
				},
				error:function (result) {
					toastr.error("错误");
				}
			})
		}
	}
</script>

</html>